package com.crm.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.crm.sys.dao.ApproveDao;
import com.crm.sys.model.ApproveRecord;
import com.crm.sys.service.ApproveService;
import com.crm.sys.service.PushService;
@Service("approveService")
public class ApproveServiceImpl implements ApproveService {
 
	@Autowired
	private ApproveDao approveDao;
 
/*	@Autowired
	private CustomerDao customerDao;
	@Autowired
	private AgreementDao agreementDao;
	@Autowired
	private BillDao billDao;*/
	@Autowired
	private PushService pushService;
	
	@Override
	public int updateApproveRecord(ApproveRecord ar,String ids) {
		/*Iterable<String> splitStr = Splitter.on(",").trimResults().omitEmptyStrings().split(ids);
		Constants.PUSHMSG="";
		for(String s :splitStr){
			ApproveRecord arResult = approveDao.getApproveRecordByID(Integer.parseInt(s));
			 //通过执行
			if(ar.getState()==2){
				//判断是否有下级审批人
				if(arResult.getMaxlevel()>arResult.getLevel()){
					 //修改下一等级状态为审批状态   该功能待定
					   
					
				}else{
					 switch (arResult.getType()) {
						case 1: //客户审批
							 //修改民称
							CustomerInfo cinfo = new CustomerInfo();
							cinfo.setId(Long.parseLong(arResult.getContentid()));
							if(arResult.getCategory()==1){
								cinfo.setCname(arResult.getNewcontent());
								customerDao.updateCustomerName(cinfo);
							}else if(arResult.getCategory()==2){
							//修改商号
								cinfo.setFirm(arResult.getNewcontent());
								customerDao.updateCustomerFirm(cinfo);
							}
							//更新审批记录状态为审批通过
							arResult.setState(ar.getState());
							approveDao.updateApproveRecord(arResult);
							break;
						case 2: //人才审批
							ResumeInfo res = new ResumeInfo();
							res.setState(2);
							res.setRstate(1);
							res.setId(Long.parseLong(arResult.getContentid()) );
							int resultCount =  resumeDao.updateResumeState(res);
							//更新审批记录状态为审批通过
							arResult.setState(ar.getState());
							approveDao.updateApproveRecord(arResult);
							break;
						case 3: //合同审批
							AgreementInfo agr = agreementDao.getAgreementById(Long.parseLong(arResult.getContentid()));
							agr.setState(2);
							if(agr.getType()==4){
								agreementDao.updateAgreementInfoDocument(agr);
							}else if(agr.getType()==5){
								agreementDao.updateAgreementInfoConfirmation(agr);
							}else if(agr.getType()==6){
								agreementDao.updateAgreementInfoWorkservice(agr);
							}else{
								 agreementDao.updateAgreementInfo(agr);
							}
							//更新审批记录状态为审批通过
							arResult.setState(ar.getState());
							approveDao.updateApproveRecord(arResult);
							break;
						case 4://签单审批
							BillInfo bf = billDao.getBillInfoByid(Long.parseLong(arResult.getContentid()));
							 switch (bf.getState()) {
							 case 0:
								    bf.setState(1);
									bf.setRstate(2);
								    billDao.updateBillInfoInfo(bf);
									//更新审批记录状态为审批通过
									arResult.setState(ar.getState());
									approveDao.updateApproveRecord(arResult);
									AgreementInfo agrinfo=agreementDao.getAgreementByUserDep(bf.getAgid());//查询签单成功消息显示内
									Constants.PUSHMSG="恭喜 "+agrinfo.getDepname()+"的"+agrinfo.getCreatename()+"以"+agrinfo.getFrontmoney()+"人民币签单成功！";
								 break;
							 case 1:
								  bf.setState(2);
									bf.setRstate(2);
								    billDao.updateBillInfoInfo(bf);
									//更新审批记录状态为审批通过
									arResult.setState(ar.getState());
									approveDao.updateApproveRecord(arResult);
									CustomerInfo cust=customerDao.getCustomerInfoById(new Object[]{bf.getCid()} );
									cust.setState(5);
									customerDao.updateCustomerState(cust);
								 break;
							 case 2:
								 break;
								 
							 }
							break;
						case 5: //合作审批
							 //修改合作状态
							CustomerInfo cinfoobj = new CustomerInfo();
							cinfoobj.setId(Long.parseLong(arResult.getContentid()));
							cinfoobj.setCoostate(2);
							cinfoobj.setState(7);
							customerDao.updateByPrimaryKeySelective(cinfoobj);
							
							//更新审批记录状态为审批通过
							arResult.setState(ar.getState());
							approveDao.updateApproveRecord(arResult);
							break;	
						case 6: //合作合同审批
							AgreementInfo agrcoo = agreementDao.getAgreementById(Long.parseLong(arResult.getContentid()));
							agrcoo.setState(2);
							if(agrcoo.getType()==4){
								agreementDao.updateAgreementInfoDocument(agrcoo);
							}else if(agrcoo.getType()==5){
								agreementDao.updateAgreementInfoConfirmation(agrcoo);
							}else if(agrcoo.getType()==6){
								agreementDao.updateAgreementInfoWorkservice(agrcoo);
							}else{
								 agreementDao.updateAgreementInfo(agrcoo);
							}
							//更新审批记录状态为审批通过
							arResult.setState(ar.getState());
							approveDao.updateApproveRecord(arResult);
							break;
						default:
							break;
					}
				}
				
			}else{ //驳回执行
				 switch (arResult.getType()) {
				 case 1: //客户审批
						
						//更新审批记录状态为驳回
						arResult.setState(ar.getState());
						arResult.setRemark(ar.getRemark());
						approveDao.updateApproveRecord(arResult);
						break;
				case 2:  //人才审批
				ResumeInfo res = new ResumeInfo();
				res.setState(4);
				res.setId(Long.parseLong(arResult.getContentid()) );
				int resultCount =  resumeDao.updateResumeState(res);
				//更新审批记录状态为驳回
				arResult.setState(ar.getState());
				arResult.setRemark(ar.getRemark());
				approveDao.updateApproveRecord(arResult);
				break;
				case 3: //合同审批
					AgreementInfo agr = agreementDao.getAgreementById(Long.parseLong(arResult.getContentid()));
					agr.setState(3);
					if(agr.getType()==4){
						agreementDao.updateAgreementInfoDocument(agr);
					}else if(agr.getType()==5){
						agreementDao.updateAgreementInfoConfirmation(agr);
					}else if(agr.getType()==6){
						agreementDao.updateAgreementInfoWorkservice(agr);
					}else{
						 agreementDao.updateAgreementInfo(agr);
					}
					//更新审批记录状态为驳回
					arResult.setState(ar.getState());
					arResult.setRemark(ar.getRemark());
					approveDao.updateApproveRecord(arResult);
					break;
				case 4:  //签单审批
					BillInfo bf = billDao.getBillInfoByid(Long.parseLong(arResult.getContentid()));
							bf.setRstate(3);
						    billDao.updateBillInfoInfo(bf);
						  //更新审批记录状态为驳回
							arResult.setState(ar.getState());
							arResult.setRemark(ar.getRemark());
							approveDao.updateApproveRecord(arResult);
					break;
				case 5: //合作审批
					 //修改合作状态
					CustomerInfo cinfoobj = new CustomerInfo();
					cinfoobj.setId(Long.parseLong(arResult.getContentid()));
					cinfoobj.setCoostate(3);
					customerDao.updateByPrimaryKeySelective(cinfoobj);
					//更新审批记录状态为驳回
					arResult.setState(ar.getState());
					arResult.setRemark(ar.getRemark());
					approveDao.updateApproveRecord(arResult);
					break;
				case 6: //合作合同审批
					AgreementInfo agrcoo = agreementDao.getAgreementById(Long.parseLong(arResult.getContentid()));
					agrcoo.setState(3);
					if(agrcoo.getType()==4){
						agreementDao.updateAgreementInfoDocument(agrcoo);
					}else if(agrcoo.getType()==5){
						agreementDao.updateAgreementInfoConfirmation(agrcoo);
					}else if(agrcoo.getType()==6){
						agreementDao.updateAgreementInfoWorkservice(agrcoo);
					}else{
						 agreementDao.updateAgreementInfo(agrcoo);
					}
					//更新审批记录状态为驳回
					arResult.setState(ar.getState());
					arResult.setRemark(ar.getRemark());
					approveDao.updateApproveRecord(arResult);
					break;
				default:
					break;
				 }
			}
			
			
			
			
		
		}
		*/
		return 0;
	}

	@Override
	public List<ApproveRecord> getApproveRecordList(ApproveRecord ar) {
		return approveDao.getApproveRecordList(ar);
	}

}
